package com.seveneight.baijiesteward.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.ocr.ui.util.DimensionUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.bean.CardBean;
import com.seveneight.baijiesteward.bean.CreditBean;
import com.seveneight.baijiesteward.bean.CreditCardListBean;
import com.seveneight.baijiesteward.http.HttpConfig;
import com.seveneight.baijiesteward.ui.main.activity.PerfectRepaymentActivity;
import com.seveneight.baijiesteward.ui.main.activity.PlanPayListActivity;
import com.seveneight.baijiesteward.ui.main.activity.RepaymentScheduleActivity;
import com.seveneight.baijiesteward.ui.main.activity.UpdateCreditCardDateActivity;

import java.util.List;

/**
 * 信用卡adapter
 * Created by user1 on 2018/5/31.
 */

public class PerfectRepaymentAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    List<CreditBean> listData;
    Activity activity;
    public PerfectRepaymentAdapter(Activity activity, List<CreditBean> listData){
        mInflater = LayoutInflater.from(activity);
        this.activity=activity;
        this.listData=listData;
    }

    @Override
    public int getCount() {
        return null==listData?0:listData.size();
    }
    public void refresh(List<CreditBean> listData){
        this.listData=listData;
        this.notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return null==listData?0:listData.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_credit_card_list_layout, null); //加载布局
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
         }
        CreditBean card=listData.get(i);
        holder.bill_date.setText("帐单日"+card.getBillDate()+"号");
        holder.repay_date.setText("还款日"+card.getRepayDate()+"号");
        holder.card_name.setText(card.getBankName());
        holder.card_number.setText("**** **** **** "+card.getCreditCardNo().substring(card.getCreditCardNo().length()-4,card.getCreditCardNo().length()));
        holder.user_name.setText(card.getRealName());
        holder.setting_default.setVisibility(card.getUnionPayCert()?View.GONE:View.VISIBLE);
        if (card.getIsDefault()==1){
//            holder.setting_default.setText("默认");
            holder.card_close.setVisibility(View.GONE);
//            holder.reservation_list.setVisibility(View.VISIBLE);
//            holder.reservation_pay.setVisibility(View.VISIBLE);
//            holder.default_hint.setVisibility(View.GONE);
        }else {
//            holder.setting_default.setText("设置默认");
            holder.card_close.setVisibility(View.VISIBLE);
//            holder.default_hint.setVisibility(View.VISIBLE);
//            holder.reservation_list.setVisibility(View.GONE);
//            holder.reservation_pay.setVisibility(View.GONE);
        }
        holder.imag_bg.getLayoutParams().height= DimensionUtil.dpToPx(card.getUnionPayCert()?157:185);
        Glide.with(activity).load(HttpConfig.SOURCE_URL+""+card.getBgUrl()).into(holder.imag_bg);
        Glide.with(activity).load(HttpConfig.SOURCE_URL+""+card.getIcoUrl()).into(holder.img_icon);
        holder.bill_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(activity, UpdateCreditCardDateActivity.class);
                intent.putExtra("cardBean",card);
                intent.putExtra("index",i);
                activity.startActivityForResult(intent,22);
            }
        });
        holder.repay_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(activity, UpdateCreditCardDateActivity.class);
                intent.putExtra("cardBean",card);
                intent.putExtra("index",i);
                activity.startActivityForResult(intent,22);
            }
        });
        holder.card_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (activity instanceof PerfectRepaymentActivity){
                    ((PerfectRepaymentActivity)activity).remove(i);
                }
            }
        });
        ViewHolder finalHolder = holder;
        holder.setting_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (finalHolder.setting_default.getText().toString().equals("设置默认")){
                    if (activity instanceof PerfectRepaymentActivity){
                        ((PerfectRepaymentActivity)activity).setdefaultCardDialog(card.getCreditCardID());
//                    }
                }
            }
        });
        holder.reservation_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.startActivity(new Intent(activity, PlanPayListActivity.class));
            }
        });
        holder.reservation_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(activity,RepaymentScheduleActivity.class);
                intent.putExtra("cardId",card.getCreditCardID());
                intent.putExtra("cardNo",card.getCreditCardNo());
                activity.startActivity(intent);
            }
        });

        return convertView;
    }

    public class ViewHolder{
        ImageView imag_bg;
        ImageView img_icon;
        TextView card_name;
        ImageView card_close;
        TextView user_name;
        TextView card_number;
        TextView bill_date;
        TextView repay_date;
        TextView setting_default;
        TextView reservation_list;
        TextView reservation_pay;
        TextView default_hint;
        LinearLayout item_credit_layout;

        ViewHolder(View view){
            imag_bg=view.findViewById(R.id.imag_bg);
            img_icon=view.findViewById(R.id.img_icon);
            card_name=view.findViewById(R.id.card_name);
            card_close=view.findViewById(R.id.card_close);
            user_name=view.findViewById(R.id.user_name);
            card_number=view.findViewById(R.id.card_number);
            bill_date=view.findViewById(R.id.bill_date);
            repay_date=view.findViewById(R.id.repay_date);
            setting_default=view.findViewById(R.id.setting_default);
            reservation_list=view.findViewById(R.id.reservation_list);
            reservation_pay=view.findViewById(R.id.reservation_pay);
            default_hint=view.findViewById(R.id.default_hint);
            item_credit_layout=view.findViewById(R.id.item_credit_layout);
        }
    };
}
